package com.ruoyi.ftrl.mqtt.config;

import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

@Configuration
public class MqttConfig {

    @Value("${mqtt.server.uri:tcp://192.168.1.61:1883}")
    private String mqttServerUri;

    @Value("${mqtt.client.id:ruoyi-mqtt-client}")
    private String mqttClientId;

    @Value("${mqtt.username:}")
    private String username;

    @Value("${mqtt.password:}")
    private String password;

    @Bean
    public MqttConnectOptions mqttConnectOptions() {
        MqttConnectOptions options = new MqttConnectOptions();

        if (username != null && !username.isEmpty()) {
            options.setUserName(username);
        }

        if (password != null && !password.isEmpty()) {
            options.setPassword(password.toCharArray());
        }

        options.setCleanSession(true);
        options.setAutomaticReconnect(true);
        options.setConnectionTimeout(10);

        return options;
    }

    @Bean
    public MqttClient mqttClient() throws MqttException {
        // 使用时间戳确保客户端ID唯一
        String clientId = mqttClientId + "_" + System.currentTimeMillis();
        MqttClient client = new MqttClient(mqttServerUri, clientId, new MemoryPersistence());

        // 打印连接前的日志
        System.err.println("正在尝试连接到MQTT代理: " + mqttServerUri);

        try {
            client.connect(mqttConnectOptions());
            System.err.println("已连接到MQTT代理: " + mqttServerUri);
        } catch (MqttException e) {
            System.err.println("无法连接到MQTT代理: " + e.getMessage());
            e.printStackTrace();
        }

        return client;
    }
//    @Bean
//    public MqttClient mqttClient() throws MqttException {
//        String brokerUrl = "tcp://localhost:1883"; // EMQX 服务器地址
//        String clientId = "TioClient"; // 客户端 ID
//
//        MqttClient client = new MqttClient(brokerUrl, clientId);
//        MqttConnectOptions options = new MqttConnectOptions();
//        options.setCleanSession(true);
//        client.connect(options); // 连接到 MQTT Broker (EMQX)
//
//        return client;
//    }




//    @Bean
//    public MqttClient mqttClient(MqttConnectOptions mqttConnectOptions) throws MqttException {
//        // 使用时间戳确保客户端ID唯一
//        String clientId = mqttClientId + "_" + System.currentTimeMillis();
//        MqttClient client = new MqttClient(
//                mqttServerUri,
//                clientId,
//                new MemoryPersistence()
//        );
//
//        // 启动时不自动连接，由服务实现连接
//        return client;
//    }


//    @Bean
//    public MqttClient mqttClient() throws Exception {
//        MqttClient client = new MqttClient(brokerUrl, clientId, new MemoryPersistence());
//        client.setCallback(new MqttCallbackImpl());
//        client.connect(mqttConnectOptions());
//        return client;
//    }
}